package com.whfc.ms.api.sys.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.entity.dto.OssPathDTO;
import com.whfc.fuum.dto.SysExternalLinkDTO;
import com.whfc.fuum.dto.SysSkinDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.param.SysSkinAdd;
import com.whfc.fuum.param.SysSkinEdit;
import com.whfc.ms.api.sys.service.MsSysConfigService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 系统外链 Controller
 *
 * @author qzexing
 * @version 1.0
 * @date 2020-10-29 15:55
 */
@RestController
@RequestMapping("/ms/api")
public class SysConfigController extends BaseController {

    @Autowired
    private MsSysConfigService sysConfigService;

    @RequestMapping(value = {"/external/link/url", "sys/link/url"})
    public Result getLinkUrl(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                             @RequestParam("deptId") Integer deptId,
                             @RequestParam("code") String code) {
        logger.info("获取系统外链地址，loginer:{},deptId:{},code:{}", user.getUsername(), deptId, code);
        SysExternalLinkDTO externalLinkDTO = sysConfigService.getLinkUrl(deptId, code, user.getId());
        return ResultUtil.success(externalLinkDTO);
    }

    @RequestMapping("/sys/skin/anon")
    public Result getSkin(@RequestParam("host") String host) {
        logger.info("获取skin，host:{}", host);
        SysSkinDTO skin = sysConfigService.getSkin(host);
        return ResultUtil.success(skin);
    }

    @RequestMapping("/sys/getSkinByDeptId")
    public Result getSkin(@RequestParam("deptId") Integer deptId) {
        logger.info("获取skin，deptId:{}", deptId);
        SysSkinDTO skin = sysConfigService.getSkinByDeptId(deptId);
        return ResultUtil.success(skin);
    }

    @RequiresPermissions(value="sys/skin")
    @GetMapping("sys/skin/list")
    public Result getSkinList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                              @RequestParam("pageNum") Integer pageNum,
                              @RequestParam("pageSize") Integer pageSize) {
        logger.info("获取skin，user:{},pageNum:{},pageSize:{}", user.getUsername(), pageNum, pageSize);
        PageData<SysSkinDTO> data = sysConfigService.getSkinList(pageNum, pageSize);
        return ResultUtil.success(data);
    }


    @RequiresPermissions(value="sys/skin")
    @PostMapping("sys/skin/add")
    public Result addSkin(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestBody @Validated SysSkinAdd param) {
        logger.info("编辑skin，user:{},param:{}", user.getUsername(), param);
        sysConfigService.addSkin(param);
        return ResultUtil.success();
    }

    @RequiresPermissions(value="sys/skin")
    @PostMapping("sys/skin/edit")
    public Result editSkin(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestBody @Validated SysSkinEdit param) {
        logger.info("编辑skin，user:{},param:{}", user.getUsername(), param);
        sysConfigService.editSkin(param);
        return ResultUtil.success();
    }

    @RequiresPermissions(value="sys/skin")
    @PostMapping("sys/skin/del/{skinId}")
    public Result delSkin(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @PathVariable("skinId") Integer skinId) {
        logger.info("删除skin，user:{},skinId:{}", user.getUsername(), skinId);
        sysConfigService.delSkin(skinId);
        return ResultUtil.success();
    }

    @RequiresPermissions(value="sys/skin")
    @PostMapping("sys/skin/img")
    public Result skinImg(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestParam("img") MultipartFile img) throws Exception {
        logger.info("skin上传图片，user:{},{}", user.getUsername(), img.getOriginalFilename());
        OssPathDTO pathDTO = sysConfigService.uploadSkinImg(img);
        return ResultUtil.success(pathDTO);
    }


}
